﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace Superbaby.PrettyUI.Forms
{
    /// <summary>
    /// Useful methods
    /// </summary>
    static class HelperMethods
    {
        /// <summary>
        /// Converts a System.Drawing.Color value into a Windows 32 color value.
        /// </summary>
        /// <param name="color">System.Drawing.Color value</param>
        /// <returns>Windows 32 integer representation of a color</returns>
        public static int ColorToWin32(Color color)
        {
            return ((color.R | (color.G << 8)) | (color.B << 16));
        }

        public static Size MeasureStringEx(Graphics g, string text, Font font, Rectangle rect)
        {
            NativeMethods.RECT bounds = new NativeMethods.RECT();
            bounds.left = rect.Left;
            bounds.right = rect.Right;
            bounds.bottom = rect.Bottom;
            bounds.right = rect.Right;

            IntPtr hFont = font.ToHfont();
            IntPtr hdc = g.GetHdc();
            IntPtr originalObject = NativeMethods.SelectObject(hdc, hFont);
            int flags = NativeMethods.DT_CALCRECT | NativeMethods.DT_WORDBREAK;
            NativeMethods.DrawText(hdc, text, -1, ref bounds, flags);
            NativeMethods.SelectObject(hdc, originalObject);
            g.ReleaseHdc(hdc);
            return new Size(bounds.right - bounds.left, bounds.bottom - bounds.top);
        }
    }
}
